

*-------------------------------------------------------------------------------
*		0. General cleaning and cleaning for long panels
*-------------------------------------------------------------------------------
qui do "${code}/Data Preparation/dataprep"

	
gen postCat = (date > mdy(3,23,2020)) + (date > mdy(7,23,2020))
label define postCat 1 "0-4 months" 2 "4+ months" 
label values postCat postCat
*replace postCat = 6 if postCat == 5 & ctry == 6


*Generate postCat dummies and mcat for long panel data
gen moy = month(date)
recode moy (1 2 = 1 "Jan-Feb") (3 4 = 2 "Mar-April") (5 6 = 3 "May-June") ///
	(7 8 = 4 "July-Aug") (9 10 = 5 "Sept-Oct") (11 12 = 6 "Nov-Dec"), gen(mcat)
gen year = date/365
tab postCat, gen(postCat)
drop postCat1

drop if ctrytext == "kenyaipush" & date > mdy(10,8,2020)
drop if ctrytext == "kenyage" & month == 52

*-------------------------------------------------------------------------------
*		1.	Add controls for seasonality samples
*-------------------------------------------------------------------------------

*				1.1 Kenya GE seasonality control
*-------------------------------------------------------------------------------
preserve
qui do "${code}/Data Preparation/dataprep_kenya_ge"
*keep if ctrytext == "kenyage"
merge m:1 date using "${raw}/kenya_ge/kenya_ge_seas", nogen keep(1 3)
gen ctrytext = "kenyage"
tempfile kenyage
save `kenyage' , replace
restore
replace _ctrl_seas = . if ctrytext == "kenyage"
merge m:1 pid date ctrytext using `kenyage', nogen update keep(1 3 4 5)
codebook _ctrl_seas if ctrytext == "kenyage"

*				1.2 Aggregating seasonality controls
*-------------------------------------------------------------------------------
egen _mctrl_seas = mean(_ctrl_seas), by(postCat ctry)
gen __ctrl_seas = _ctrl_seas if date < mdy(3,1,2020)
replace __ctrl_seas = _mctrl_seas if date > mdy(3,1,2020)
replace __ctrl_seas = -1*__ctrl_seas
label var __ctrl_seas "Seasonal Food Security"


*				1.3 Linear time control __year for kenyaipush
*-------------------------------------------------------------------------------
gen __year = date/365
egen myear = mean(__year), by(postCat ctry)
replace __year = myear if postCat > 0



*-------------------------------------------------------------------------------
*		2		Generate Country-Level Estimation Results
*-------------------------------------------------------------------------------

*			2.1		Empty variables to fill
*-------------------------------------------------------------------------------

*Point estimates and SEs for country specific estimation results
foreach t in 2 3 {
	foreach type in "" _long _val { 
		gen pc`t'`type' = .
		gen pcse`t'`type' = .
		if `t' == 3 gen pcdif`type' = .
		if `t' == 3 gen pcsedif`type' = .
	}
}
gen plot_ctry = ""
gen index = ""
gen mod = ""

*		2.2		Estimates with no controls (basic) and preferred estimates (best)
*					for each sample
*-------------------------------------------------------------------------------
gl ctrys kenyaipush kenyage  nepal nigeria bangladesh sierraleone colombia rwanda kenyaklps drc
loc i = 0
qui foreach m in best basic { //
	qui foreach s in _fw _nw _icw {
		qui foreach c in $ctrys {
			loc ++ i
			loc ctrl year i.mcat
			if regexm("`c'", "(nigeria|bangladesh|sierraleone|drc)") loc ctrl 
			if regexm("`c'", "(nepal|kenyage)") loc ctrl __ctrl_seas
			if "`c'" == "kenyaipush" loc ctrl __year

			noi di "---------------------------------------"
			noi di "		`s': `c'	: `m'	"
			noi di "---------------------------------------"
			if "`m'" == "basic" loc ctrl 
			noi areg ___depression`s' `ctrl' i.postCat if ctrytext == "`c'", absorb(pid) vce(cluster hhid)

			replace mod = "`m'" if _n == `i'
			replace plot_ctry = "`c'" if _n == `i'
			replace index = "`s'" if _n == `i'
			cap replace pc2 = _b[1.postCat] if _n == `i'
			cap replace pcse2 = _se[1.postCat] if _n == `i'
			cap replace pc3 = _b[2.postCat] if _n == `i'
			cap replace pcse3 = _se[2.postCat] if _n == `i'
			
			noi areg ___depression`s' `ctrl' ib1.postCat if ctrytext == "`c'", absorb(pid) vce(cluster hhid)
			cap replace pcdif = _b[2.postCat] if _n == `i'
			cap replace pcsedif = _se[2.postCat] if _n == `i'

		}
		
		
		
	}
}

foreach pre in pc2 pc3 pcse2 pcse3 pcdif pcsedif {
			replace `pre'_long = `pre' 		
			replace `pre'_long = . if regexm(plot_ctry, "(ipush|sierra|bang|drc|nigeria)") 
			replace `pre'_val = `pre' if regexm(plot_ctry, "(bangladesh|colombia|nigeria|drc|kenyaipush)") 
}

*		2.3		RD Estimates for select samples
*-------------------------------------------------------------------------------
gen pcse2_rd = pcse2
gen pc2_rd = pc2
qui foreach s in _fw _nw _icw {
qui foreach c in  colombia kenyaipush kenyaklps nepal rwanda  { //
		tempfile main
		save `main', replace
		use "${output}\clean\stringency_cases_clean.dta", clear
		keep if ctrytext == "`c'"
		*net install rdrobust, from(https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata) replace
		egen mdepression = mean(depression), by(pid)
		bysort pid (days_post_maxChange): gen dep_base = ___depression`s'[1]
		*if "`c'" == "kenyaipush" keep if round != 0
		noi di "----------------------------------------------------"
		noi di "------                    `c'                   ----"
		noi di "----------------------------------------------------"
		*bysort pid (days_post_maxChange): keep if _N > 1
		gen moy = month(date)
		recode moy (1 2 = 1 "Jan-Feb") (3 4 = 2 "Mar-April") (5 6 = 3 "May-June") ///
			(7 8 = 4 "July-Aug") (9 10 = 5 "Sept-Oct") (11 12 = 6 "Nov-Dec"), gen(mcat)
			tab mcat, gen(mcat)
			
		if "`c'" == "nepal" | "`c'" == "kenyaipush" noi eststo `c'`s': rdrobust ___depression`s' ///
			days_post_maxChange , masspoints(adjust) covs(dep_base) 
		if "`c'" == "rwanda" noi eststo `c'`s': rdrobust ___depression`s' days_post_maxChange , masspoints(adjust) ///
			covs(dep_base) h(700) b(700) 
		if "`c'" == "kenyaklps" noi eststo `c'`s': rdrobust ___depression`s' days_post_maxChange ///
			, masspoints(adjust) h(900) 
		if "`c'" == "colombia" noi eststo `c'`s': rdrobust ___depression`s' ///
			days_post_maxChange , masspoints(off) h(755) 
		
		use `main', clear
		replace pc2_rd = _b[RD_Estimate] if plot_ctry == "`c'" & index == "`s'"
		replace pcse2_rd = _se[RD_Estimate] if plot_ctry == "`c'" & index == "`s'"
		save `main', replace
	}
}


replace pc3 = . if plot_ctry == "kenyaipush"
replace pc2_long = . if plot_ctry == "kenyaipush"
replace pc3_long = . if plot_ctry == "kenyaipush"
replace pcdif_long = . if plot_ctry == "kenyaipush"
replace pcdif = . if plot_ctry == "kenyaipush"
replace pcdif_long = . if plot_ctry == "nigeria"
replace pcdif = . if plot_ctry == "nigeria"


*-------------------------------------------------------------------------------
*		3		Aggregate Estimation Results
*-------------------------------------------------------------------------------

gen metaES = .
gen metaSE = .
gen metaKI = ""
gen metaIndex = ""
gen metaT = .
gen metaP = .

gen metaES_dif = .
gen metaSE_dif = .
gen metaP_dif = .
loc i = 0
qui foreach m in best basic  { // 
	foreach type in "" _long _val { //   All, long panels only, validated measures only
		foreach s in  _nw _fw _icw { //index 
		
		noi di "---- m: `m'  ;  type:  `type'   ;  s:  `s'   ----"
		if "`type'" == "" {
				replace pcdif`type' = . if pcdif`type' == 0
				replace pcsedif`type' = . if pcsedif`type' == 0
				metaan pcdif`type' pcsedif`type' if index == "`s'" & mod == "`m'" , label(plot_ctry) reml forest 
				graph export	"${output}/forPaper/Supplemental Forest Plots/metadif`type'`s'`msuf'.png", replace
				replace metaES_dif = `r(eff)' if _n == `i' + 1
				replace metaSE_dif = sqrt(`r(effvar)') if _n == `i' + 1
				replace metaP_dif = 2*(1-normal(abs(`r(eff)'/sqrt(`r(effvar)')))) if _n == `i' + 1
			}
			

			foreach t in 2 3 { //time period
				if `t' == 3 & "`type'" == "_rd" continue //No RD for period 2 (4 + months post covid)
				if "`m'" == "basic" & "`type'" != "" continue //Include all samples, no controls for "basic"
				loc msuf 
				if "`m'" == "basic" loc msuf _basic
				loc ++ i
				*continue
				replace pc`t'`type' = . if pc`t'`type' == 0
				replace pcse`t'`type' = . if pcse`t'`type' == 0
						noi di " `m' : `type' : `s' : `t'"

		loc est_method reml
		if "`type'" == "_val" loc est_method dl // REML fails to converge with only two observations in validated, short-run sample
				metaan pc`t'`type' pcse`t'`type' if index == "`s'" & mod == "`m'" , label(plot_ctry) `est_method' forest
				graph export	"${output}/forPaper/Supplemental Forest Plots/meta`type'`s'`t'`msuf'.png", replace
				
				replace metaES = `r(eff)' if _n == `i'
				replace metaSE = sqrt(`r(effvar)') if _n == `i'
				replace metaP = 2*(1-normal(abs(metaES[`i']/metaSE[`i']))) if _n == `i'
				replace metaKI = "Best Estimates"*("`type'" == "") +  ///
								"Best Estimates, Long Panels Only"*("`type'" == "_long") + ///
								"RD Estimates"*("`type'" == "_rd") + ///
								"Validated Measures Only"*("`type'" == "_val") if _n == `i'
				replace metaKI = "Basic Pre-Post" if ("`m'" == "basic") &  _n == `i' 
				replace metaIndex = "`s'" if _n == `i'
				replace metaT = `t' if _n == `i'
			}
		}
	}
}
cap label define metaT 2 "0-4 Months" 3 "4+ Months"
label values metaT metaT 
replace metaT = metaT + 0.1*(metaIndex == "_nw") + 0.2*(metaIndex == "_icw")
cap drop metaHI
cap drop metaLO
gen metaHI = metaES + 1.95*metaSE
gen metaLO = metaES - 1.95*metaSE

preserve
keep metaES metaHI metaLO metaSE metaP mod metaIndex metaKI metaT metaP_dif metaES_dif metaSE_dif
keep if !mi(metaES)
export excel using "${output}\forPaper\Supplemental Forest Plots\meta_all.xls", replace first(var)
restore


preserve
keep if regexm(metaKI, "(Best|Validated)")
replace metaKI = "All Samples" if metaKI == "Best Estimates"
replace metaKI = "Long Samples Only" if regexm(metaKI, "Long")
replace metaES = . if regexm(metaKI, "Validated") & metaIndex != "_nw"
replace metaLO = . if regexm(metaKI, "Validated") & metaIndex != "_nw"
replace metaHI = . if regexm(metaKI, "Validated") & metaIndex != "_nw"
tw (scatter metaES metaT if metaIndex == "_fw", color(gs0)) (rcap metaLO metaHI metaT if metaIndex == "_fw", color(gs0)) ///
 (scatter metaES metaT if metaIndex == "_nw", color(gs5)) (rcap metaLO metaHI metaT if metaIndex == "_nw", color(gs5)) ///
 (scatter metaES metaT if metaIndex == "_icw", color(gs10)) (rcap metaLO metaHI metaT if metaIndex == "_icw", color(gs10)), ///
	by(metaKI, graphregion(color(white)) note("") ) ///
	legend(order(1 "Factor Index" 3 "Unweighted Index" 5 "Inverse-Covariance Index")) ///
	xlabel(1.5 " " 2 "0-4 Months" 3 "4+ Months" 3.5 " ") yline(0, lpattern(".")) graphregion(color(white)) ///
	xtitle(Time Post COVID) 
			graph export	"${output}/forPaper/Fig2.png", replace			
restore



preserve
keep if regexm(metaKI, "(Basic)")
replace metaES = . if regexm(metaKI, "Validated") & metaIndex != "_nw"
replace metaLO = . if regexm(metaKI, "Validated") & metaIndex != "_nw"
replace metaHI = . if regexm(metaKI, "Validated") & metaIndex != "_nw"
tw (scatter metaES metaT if metaIndex == "_fw", color(gs0)) (rcap metaLO metaHI metaT if metaIndex == "_fw", color(gs0)) ///
 (scatter metaES metaT if metaIndex == "_nw", color(gs5)) (rcap metaLO metaHI metaT if metaIndex == "_nw", color(gs5)) ///
 (scatter metaES metaT if metaIndex == "_icw", color(gs10)) (rcap metaLO metaHI metaT if metaIndex == "_icw", color(gs10)), ///
	legend(order(1 "Factor Index" 3 "Unweighted Index" 5 "Inverse-Covariance Index")) ///
	xlabel(1.5 " " 2 "0-4 Months" 3 "4+ Months" 3.5 " ") yline(0, lpattern(".")) graphregion(color(white)) ///
	xtitle(Time Post COVID) 
			graph export	"${output}/forPaper/FigS3.png", replace
restore






